'use strict';

import Base from './base.js';

export default class extends Base {
	
	/*关闭一个连接*/
	closeAction(self){
		var socket = self.http.socket;
		var op = this.cookie(socket.id);
		var openid = think.cache("111");
		 console.log("colse:"+openid)
		// this.broadcast('connection', {status: 0,openid:'123'});
	}
	conneAction(self){
		var socket = self.http.socket;
		var data = self.http.data;
		console.log(data)
		this.broadcast('connection', {status: 0,message: "Fail",openid:data.openid});
	}
	/*用户连接*/
	*userlistAction(self){

		var socket = self.http.socket;
		var data = self.http.data;
		var is_service = data.service == 0 ? 1 : 0;
		var app_id = data.appid;
		var openid = data.openid;
		let model = this.model("members");
		let base  = null;
		if(is_service == 1){
			base = yield model.where({appId:app_id,service:is_service}).select();
		}else{
			base = yield this.model("massges").userList({appId:app_id,to_openid:openid});
		}
		var rel = this.setSocket(openid,socket.id);
		if(rel === true){
			think.cache("111", {openid:openid});
			console.log("open:"+openid)
			var f = think.cache("111").then();
			console.log("open 222- :"+f)
			this.broadcast('connection', {status: 1,message: "OK",openid:openid});	
		}else{
			this.broadcast('connection', {status: 0,message: "Fail",openid:openid});
		}
		for(var k in base){
			var csocket = this.getSocket(base[k].openid);
			if(csocket){
				base[k].linestr = "在线";
			}else{
				base[k].linestr = "离线";
			}
			// /*处理消息*/
			// var where = "appId="+app_id;
			// where +=" and ((openid='"+openid+"' and to_openid='"+base[k].openid+"') or (to_openid='"+openid+"' and openid='"+base[k].openid+"'))";
			// let massge = yield this.model('massges').where(where).order("addtime desc").find();
			// if(!think.isEmpty(massge)){
			// 	base[k].text = massge.text;
			// 	base[k].addtime = massge.addtime;
			// 	base[k].isRead = massge.isRead;
			// 	if(openid == massge.openid){
			// 		base[k].isRead = 1;
			// 	}
			// }else{
			// 	base[k].text = null;
			// 	base[k].addtime = null;
			// 	base[k].isRead = 1;
			// }
		}
		global.sort(base);
		this.emit('userlist',{status:1,info:base});
	}
	/*发送消息给对方*/
	*chatAction(self){
		var socket = self.http.socket;
		var data = self.http.data;
		if(think.isEmpty(data.text)){
			this.emit('chat',{code:2001,err:'不可发送空消息.'});
		}
		if(think.isEmpty(data.to_openid)){
			this.emit('chat',{code:2002,err:'未知错误.'});
		}
		let model = this.model("massges");
		let insertId = yield model.add({text: data.text,appId:data.appId,openid:data.openid,to_openid:data.to_openid,addtime:data.ctime,nickname:data.nickname,to_nickname:data.to_nickname});
		var tosocket = '';
		console.log(data)
		if(insertId>0){
            var tosocket = this.getSocket(data.to_openid);
            /*发给接受人，如果在线*/
            if(tosocket){
            	socket.emit('chat',{code:0,'err':'在线...'});
            	let user = yield this.model("members").where({'openid':data.openid,'appId':data.appId}).find();
            	console.log(user)
            	/*昵称或临时昵称*/
            	var nickname = user.nickname;
            	if(think.isEmpty(nickname)){
            		var socketaArr = thinkCache(thinkCache.WEBSOCKET,'MYWEBSOCKETS');
					nickname = socketaArr.length+1000;
            	}
            	/*头像*/
            	var avatar = user.headvar;
            	if(think.isEmpty(avatar)){
            		avatar = "static/img/h.png";	
            	}
                tosocket.emit('accept',{code:0,form_openid:data.openid,text:data.text,nickname:nickname,avatar:avatar,addtime:data.ctime,openid:data.to_openid})
            }else{
            	socket.emit('chat',{code:404,'err':'不在线...'});
            	//接收人不在线逻辑处理，返回给微信第三方处理，发送微信端信息通知
            }
			return '';
		}
		socket.emit('chat',{code:2002,'err':'未知错误.'});
		console.log(data)
	}
	/*获取到消息*/
	*massgesAction(self){
		var socket = self.http.socket;
		var data = self.http.data;
		let massges = this.model('massges').where({}).select();
		console.log(data)
	}
}